<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>按键列表</span>
        <el-button
          size="mini"
          style="width:100px;float: right; margin:-5px 20px 0 0 "
          type="primary"
          @click="addKey"
        >新增</el-button>
      </div>
      <el-row :gutter="10">
        <el-form label-width="80px">
          <el-col :span="5">
            <el-form-item label="按键ID">
              <el-input v-model="addId" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="触发函数名" label-width="100px">
              <el-input v-model="addFn" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="菜单ID">
              <el-input v-model="addMenuId" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="按键名称">
              <el-input v-model="addBtnName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2" :pull="1">
            <el-form-item>
              <el-button size="small" type="primary" plain @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>
    <el-table :data="tableData" style="width: 100%;margin-top:20px;">
      <el-table-column align="center" label="按键ID" prop="id"></el-table-column>
      <el-table-column align="center" label="触发函数名" prop="wfFunction"></el-table-column>
      <el-table-column align="center" label="菜单ID" prop="menuId"></el-table-column>
      <el-table-column align="center" label="菜单名称" prop="menuName"></el-table-column>
      <el-table-column align="center" label="按键名称" prop="name"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog center title="按键编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-row>
          <el-col :span="11">
            <el-form-item label="按键ID" :label-width="formLabelWidth">
              <el-input disabled size="mini" v-model="form.id" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="按键名称" :label-width="formLabelWidth">
              <!-- <el-input size="mini" v-model="form.name" autocomplete="off"></el-input> -->
              <el-select size="mini" v-model="form.name" placeholder="请按钮名称">
                <el-option
                  v-for="item in buttonOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-select size="mini" filterable v-model="form.region" placeholder="请菜单名称">
            <el-option
              v-for="item in menuOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-col :span="24">
          <el-form-item label="触发函数名" :label-width="formLabelWidth">
            <el-input size="mini" v-model="form.fn" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveFn">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this._initDefault();

    this.$post("/menu/list", {
      pageNum: 1,
      pageSize: 1000,
      proceedingId:this.$route.query.itemId
    }).then(data => {
      this.menuOptions = data.result.dataList;
    });
  },
  data() {
    return {
      tableData: [],
      addId: "",
      addFn: "",
      addMenuId: "",
      addBtnName: "",
      menuName: [],
      menuOptions: "",
      dialogFormVisible: false,
      buttonOptions: [
        {
          name: "上一步"
        },
        {
          name: "下一步"
        },
        {
          name: "流转"
        },
        {
          name: "最大化"
        },
        {
          name: "刷新"
        }
      ],
      form: {
        id: "",
        fn: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        name: ""
      },
      formLabelWidth: "120px"
    };
  },
  methods: {
    _initDefault() {
      this.$post("/button/list", {
        pageSize: 1000,
        pageNum: 1,
        proceedingId: this.$route.query.itemId
      }).then(data => {
        
        this.tableData = data.result.dataList;
      });
    },
    search() {
      this.$post("/button/list", {
        pageSize: 1000,
        pageNum: 1,
        itemId: this.$route.query.itemId,
        id: this.addId,
        menuId: this.addMenuId,
        name: this.addBtnName,
        wfFunction: this.addFn
      }).then(data => {
        
        this.tableData = data.result.dataList;
      });
    },
    handleEdit(index, row) {
      this.form.id = row.id;
      this.form.name = row.name;
      this.form.fn = row.wfFunction;
      this.form.region = row.menuId;
      this.dialogFormVisible = true;
    },
    addKey() {
      this.dialogFormVisible = true;
      this.form = {
        id: "",
        fn: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        name: ""
      };
    },
    handleDelete(index, row) {
      this.$post("/button/delete", {
        id: row.id
      }).then(data => {
        if (data) {
          this._initDefault();
          this.$message({
            type: "success",
            message: data.resultDes
          });
        }
      });
    },
    saveFn() {
      this.$post("/button/save", {
        id: this.form.id ? this.form.id : null,
        menuId: this.form.region,
        name: this.form.name,
        wfFunction: this.form.fn
      }).then(data => {
        if (data) {
          this._initDefault();
          this.$message({
            type: "success",
            message: data.resultDes
          });
          this.dialogFormVisible = false;
        }
      });
    }
  }
};
</script>

<style scoped lang="css">
</style>
